package com.blue.wechat.receive.parse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.blue.wechat.receive.model.AbstractResponse;
import com.blue.wechat.receive.model.ArticleItem;
import com.blue.wechat.receive.model.ImageResponse;
import com.blue.wechat.receive.model.MusicResponse;
import com.blue.wechat.receive.model.NewsResponse;
import com.blue.wechat.receive.model.TextResponse;
import com.blue.wechat.receive.model.VideoResponse;
import com.blue.wechat.receive.model.VoiceResponse;

public class JsonModelBuilder
{
	private JsonModelBuilder()
	{
	}
	
	
	public static String build(AbstractResponse response)
	{
		if (response == null)
			return "";
		
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("touser", response.getToUserName());
		root.put("msgtype", response.getType().getText());
		
		if (response instanceof TextResponse)
		{
			buildText(root, (TextResponse)response);
		}
		else if (response instanceof NewsResponse)
		{
			buildNews(root, (NewsResponse)response);
		}
		else if (response instanceof ImageResponse)
		{
			buildImage(root, (ImageResponse)response);
		}
		else if (response instanceof MusicResponse)
		{
			buildMusic(root, (MusicResponse)response);
		}
		else if (response instanceof VoiceResponse)
		{
			buildVoice(root, (VoiceResponse)response);
		}
		else if (response instanceof VideoResponse)
		{
			buildVideo(root, (VideoResponse)response);
		}
		
		return JSON.toJSONString(root);
	}


	private static void buildText(Map<String, Object> root, TextResponse response)
	{
		Map<String, Object> text = new HashMap<String, Object>();
		root.put("text", text);
		
		text.put("content", response.getContent());
	}


	private static void buildNews(Map<String, Object> root, NewsResponse response)
	{
		Map<String, Object> news = new HashMap<String, Object>();
		root.put("news", news);
		
		List<Map<String, String>> articles = new ArrayList<Map<String, String>>();
		news.put("articles", articles);
		
		for (ArticleItem item : response.getItemList())
		{
			Map<String, String> map = new HashMap<String, String>();
			map.put("title", item.getTitle());
			map.put("description", item.getDescription());
			map.put("url", item.getUrl());
			map.put("picurl", item.getPicUrl());
			articles.add(map);
		}
	}


	private static void buildMusic(Map<String, Object> root, MusicResponse response)
	{
		Map<String, String> music = new HashMap<String, String>();
		root.put("music", music);
		
		music.put("title", response.getTitle());
		music.put("description", response.getDescription());
		music.put("musicurl", response.getMusicUrl());
		music.put("hqmusicurl", response.getHqMusicUrl());
		music.put("thumb_media_id", response.getThumbMediaId());
	}


	private static void buildVoice(Map<String, Object> root, VoiceResponse response)
	{
		Map<String, String> voice = new HashMap<String, String>();
		root.put("voice", voice);
		
		voice.put("media_id", response.getMediaId());
	}


	private static void buildImage(Map<String, Object> root, ImageResponse response)
	{
		Map<String, String> voice = new HashMap<String, String>();
		root.put("image", voice);
		
		voice.put("media_id", response.getMediaId());
	}


	private static void buildVideo(Map<String, Object> root, VideoResponse response)
	{
		Map<String, String> video = new HashMap<String, String>();
		root.put("video", video);
		
		video.put("media_id", response.getMediaId());
		video.put("title", response.getTitle());
		video.put("description", response.getDescription());
	}
}
